package com.LeetCode.MapAndList;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * LRU缓存
 */
public class LRUCache {

    LinkedHashMap<Integer,Integer> cache;

    int capacity = 0;

    public LRUCache(int capacity){
        this.capacity = capacity;
        cache = new LinkedHashMap<Integer, Integer>(capacity,0.75f,true){
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest){
                return cache.size() > capacity;
            }
        };
    }

    public void put(int key ,int value){
        cache.put(key,value);
    }

    public int get(int key){
        return cache.getOrDefault(key, -1 );
    }

    public static void main(String[] args){
        LRUCache cache = new LRUCache(3);
        cache.put(1, 1);
        cache.put(2, 2);
        cache.put(3, 2);
        System.out.println(cache.get(1));
        cache.put(4, 4);
        System.out.println(cache.get(2));
    }


}
